package com.shenjianquan.cryptography.handel;

public class Affine {
    public  String deciphering(String s,int a, int b){// 解密的实现
        char[] ch = s.toCharArray();
        int length = ch.length;// 密文长度
        int[] in = new int[length];
        for (int i = 0; i < ch.length; i++) {
            in[i] = ch[i] - 97;// 利用ascii变成0-25数字
        }
        for (int i = 0; i < length; i++) {
            in[i] = ((in[i] - b) * a) % 26;// 解密算法
            if (in[i] < 0) in[i] += 26;
        }
        for (int i = 0; i < ch.length; i++) {
            ch[i] = (char) (in[i] + 97);// 将数字变成字母
        }
        return String.valueOf(ch);// 将字符串数字变成String类型的字符串，返回
    }

 /*   public static void main(String[] args) {
        Arithmetic arithmetic = new Arithmetic();
        final int MOD = 26;
        int [] gcd = new int[12];
        int m = 0;
        String out = null;
        for(int i=1;i<MOD;i++){
            if((arithmetic.euclid(i,MOD))==1) {   //求与26互素的数
                gcd[m] = (arithmetic.euclid_2(i,MOD)+26)%26; //求这些数mod26的逆,并把它加入gcd数组
                m++;
            }
        }
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();// 输入密文
        Affine affine = new Affine();
        int k =1;
        for(int i=0;i<12;i++) {
            for (int j = 0; j < 26; j++) {
                out = affine.deciphering(s, gcd[i], j);
                System.out.println("第"+k+"条明文为："+out);
                k++;
            }
        }
    }
*/
}

